package behaviorlearning;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;

import util.XMLWriter;


public class WargusTraceConverter {
		
	public static void main(String args[]) {		
		try	{
			BufferedReader in = new BufferedReader (new FileReader(args[0]));
			XMLWriter writer = new XMLWriter(new PrintWriter(new FileWriter(args[1])));
			BufferedReader in_goals = null;
			if (args.length >= 3) {
				in_goals = new BufferedReader(new FileReader(args[2]));
			}

			translate_Wargus_trace(in, writer, in_goals);

			if (in_goals!=null) in_goals.close();
			in.close();
			writer.close();
		} catch(Exception e) {
			if (args.length<2) {
				System.out.println("Usage: WargusTraceConverter <in-log-file> <out-xml-file> [<goal-annotation-file>]");
			} else {
				System.out.println("Exception:" + e);
				e.printStackTrace();
			} // if 
		}
	}
	
	static void translate_Wargus_trace(BufferedReader in, XMLWriter w, 
			BufferedReader in_goal) throws Exception {
		List<Case> cases = WargusTraceLoader.load_Wargus_trace(in, in_goal);
		
		w.rawXML("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		w.tag("caselibrary xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
			"  xsi:noNamespaceSchemaLocation=\"Wargus-casebase.xsd\""); 
		{
			for(Case c : cases) c.writeXML(w);
		}
		w.tag("/caselibrary");
	}

}
